/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import pojo.DimensionData;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Roaa
 */
public class DimensionDataDao {

    private Connection con;
    
    public DimensionDataDao(Connection con) {
        this.con=con;
    }

    //update data in dimension data table
    public boolean update(DimensionData bData) {
        int f = 0;
        boolean result = false;
        try {
            PreparedStatement stmt = con.prepareStatement("UPDATE dimensiondata SET length=?, lengthby=?, width=?, widthby=?, depth=?, depthby=?, weight=?, weightby=?, pages=?, pagesby=?, format=?, formatby=?, mailformat=?, mailformatby=?, items=?, itemsby=?, recupdatedate=? WHERE ISBN13= ?");
            stmt.setLong(18, bData.getIsbn13());
            stmt.setInt(1, bData.getLength());
            stmt.setShort(2, bData.getLengthBy());
            stmt.setInt(3, bData.getWidth());
            stmt.setShort(4, bData.getWidthBy());
            stmt.setInt(5, bData.getDepth());
            stmt.setShort(6, bData.getDepthBy());
            stmt.setInt(7, bData.getWeight());
            stmt.setShort(8, bData.getWeightBy());
            stmt.setInt(9, bData.getPages());
            stmt.setShort(10, bData.getPagesBy());
            stmt.setString(11, bData.getFormat());
            stmt.setShort(12, bData.getFormatBy());
            stmt.setString(13, bData.getMailFormat());
            stmt.setShort(14, bData.getMailFormatBy());
            stmt.setInt(15, bData.getItems());
            stmt.setShort(16, bData.getItemsBy());
            stmt.setDate(17, new Date(bData.getRecUpdateDate().getTime()));
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimensionDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    //insert data into dimension data table
    public boolean insert(DimensionData bData) {
        int f = 0;
        boolean result = false;
        try {
            PreparedStatement stmt = con.prepareStatement("INSERT INTO dimensiondata VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            stmt.setLong(1, bData.getIsbn13());
            stmt.setInt(2, bData.getLength());
            stmt.setShort(3, bData.getLengthBy());
            stmt.setInt(4, bData.getWidth());
            stmt.setShort(5, bData.getWidthBy());
            stmt.setInt(6, bData.getDepth());
            stmt.setShort(7, bData.getDepthBy());
            stmt.setInt(8, bData.getWeight());
            stmt.setShort(9, bData.getWeightBy());
            stmt.setInt(10, bData.getPages());
            stmt.setShort(11, bData.getPagesBy());
            stmt.setString(12, bData.getFormat());
            stmt.setShort(13, bData.getFormatBy());
            stmt.setString(14, bData.getMailFormat());
            stmt.setShort(15, bData.getMailFormatBy());
            stmt.setInt(16, bData.getItems());
            stmt.setShort(17, bData.getItemsBy());
            stmt.setDate(18, new Date(bData.getRecUpdateDate().getTime()));
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimensionDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    //
    public boolean delete(long isbn13) {
        int f = 0;
        boolean result = false;
        try {
            PreparedStatement stmt = con.prepareStatement("DELETE FROM dimensiondata WHERE ISBN13=?");
            stmt.setLong(1, isbn13);
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimensionDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }
    
    //get All Data Using ISBN
    public Vector<DimensionData> getByISBN(long isbn){
        try {
            Vector<DimensionData> ddvect = new Vector<DimensionData>();
            PreparedStatement ps = con.prepareStatement("select * from dimensiondata where isbn13 = ? ");
            ps.setLong(1, isbn);
            ResultSet rset = ps.executeQuery();
            while (rset.next()) {
                DimensionData dd = new DimensionData();
                dd.setIsbn13(rset.getLong(1));
                dd.setLength(rset.getInt(2));
                dd.setLengthBy(rset.getShort(3));
                dd.setWidth(rset.getInt(4));
                dd.setWidthBy(rset.getShort(5));
                dd.setDepth(rset.getInt(6));
                dd.setDepthBy(rset.getShort(7));
                dd.setWeight(rset.getInt(8));
                dd.setWeightBy(rset.getShort(9));
                dd.setPages(rset.getInt(10));
                dd.setPagesBy(rset.getShort(11));
                dd.setFormat(rset.getString(12));
                dd.setFormatBy(rset.getShort(13));
                dd.setMailFormat(rset.getString(14));
                dd.setMailFormatBy(rset.getShort(15));
                dd.setItems(rset.getInt(16));
                dd.setItemsBy(rset.getShort(17));
                dd.setRecUpdateDate(rset.getDate(18));
                ddvect.add(dd);
            }
            return ddvect;
        } catch (SQLException ex) {
            Logger.getLogger(ProviderDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
